package com.kaifamiao.tracking.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/session/obtain")
public class SessionObtainServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 获得与当前请求关联的会话对象
        HttpSession session = request.getSession();
        System.out.println( session );

        response.setCharacterEncoding( "UTF-8" );
        response.setContentType( "text/html;charset=UTF-8" );
        PrintWriter out = response.getWriter();

        out.println( "<div style='text-align: center'>" );
        out.println( "<p> session id : " +  session.getId() + "</p>");
        out.println( "<p> session is new : " +  session.isNew() + "</p>");
        out.println( "</div>" );

    }

}
